package com.aier.school.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.dao.ICommonDAO;
import com.aier.school.common.SchoolFinalPageSize;
import com.aier.school.entity.AssignMessageRecord;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolGrade;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.service.ISchoolService;
import com.aier.school.service.ISchoolTeacherService;
import com.aier.user.common.UserFinalPageSize;
import com.aier.user.service.IUserBuyService;

@Scope("prototype")
@Component("schoolMessageManageAction")
public class SchoolMessageManageAction extends SchoolBaseAction{

    /**
     * 学校管理-短信管理
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "schoolService")
    private ISchoolService schoolService;    
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;        
    @Resource(name = "userBuyService")
    private IUserBuyService userBuyService;
    
    private int s;                //成功:1 ,失败:0
        
    private Integer sid;        //学校id
    private List<String> tnameAccountList;        //教师名字账号列表
    private List<SchoolGrade> schoolGradeList;    //职务列表    
    private List<SchoolTeacher> schoolTeacherList;        //学校教师列表                 
    private Long tid;            //教师id
    private Long gid;            //职务id
    private String account;        //教师账号
    
    private int page;            //页数
    private int page_size;        //每页的记录数
    private int page_count;        //总页数
    private int count;            //总数
    
    
    private School school; // 学校实体
    private String mesSuffix; // 学校短信后缀
    private Integer mesCount; // 短信条数
    private List<AssignMessageRecord> assignHistoryList; // 学校分配给教师的短信历史记录
    
    
    @Override
    public String execute(){
        // 判断学校的付费方式（1按月购买2按短信条数购买）
        school = userBuyService.searchSchoolMessageCountSid(sid);
        
        tnameAccountList = schoolTeacherService.searchTNameAndAccountBySid(sid);
        tnameAccountList.add(0, "所有教师");
        schoolGradeList = schoolService.searchSchoolGradeList(sid);
        
        return SUCCESS;
    }
    
    /**
     * 查询学校教师
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchSchoolTeacherList(){
        s = 1;    
        Map<String,Object> map = schoolTeacherService.searchSchoolTeacherList(page, 
                SchoolFinalPageSize.CLASSES_MAINTAIN_LIST_PAGESIZE, sid,account,gid);
        if(null==map){
            s = 0;
        }
        schoolTeacherList = (List<SchoolTeacher>) map.get(ICommonDAO.OBJ_LIST);
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;
    }
    
    /**
     * 修改学校短信后缀
     * @return
     */
    public String updateMessageSuffix() {
        s = schoolService.updateMessageSuffix(sid, mesSuffix);
        return SUCCESS;
    }
    
    /**
     * 给教师分配短信
     * @return
     */
    public String assignMessage() {
        s = schoolTeacherService.updateTeacherMessage(tid, mesCount,sid);
        school = userBuyService.searchSchoolMessageCountSid(sid);
        return SUCCESS;
    }
    
    /**
     * 查询学校分配短信的历史记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchAssignList() {
       
        if (page <= 0) {
            page = 1;
        }
        Map<String, Object> map = schoolTeacherService.searchAssignList(tid,page, UserFinalPageSize.PROXY_SCHOOLS_PAGESIZE);
        assignHistoryList =  (List<AssignMessageRecord>) map.get(ICommonDAO.OBJ_LIST);
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        s=1;
        return SUCCESS;
    }

    
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getS() {
        return s;
    }

    public List<SchoolGrade> getSchoolGradeList() {
        return schoolGradeList;
    }

    public List<SchoolTeacher> getSchoolTeacherList() {
        return schoolTeacherList;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setUserBuyService(IUserBuyService userBuyService) {
        this.userBuyService = userBuyService;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public void setGid(Long gid) {
        this.gid = gid;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public List<String> getTnameAccountList() {
        return tnameAccountList;
    }

    public Integer getSid() {
        return sid;
    }

    public School getSchool() {
        return school;
    }

    public void setMesSuffix(String mesSuffix) {
        this.mesSuffix = mesSuffix;
    }

    public void setMesCount(Integer mesCount) {
        this.mesCount = mesCount;
    }

    public List<AssignMessageRecord> getAssignHistoryList() {
        return assignHistoryList;
    }

}
